import {observable, action, toJS} from 'mobx';
import * as API from '../../api/systemdoc';
import { SearchPage,ListViewPage } from 'weaver-mobile-page';
import AddStore from '../docForm/AddStore';

export default class DocListStore {
  
  systemDocStore = null;
  
  docList = [];
  @observable searchKey = {};  //搜索条件
  searchType = 5;  //搜索类型

  // 列表模式下文档列表的页码
  docPageNum = 0;
  @observable docListViewPageStore = new ListViewPage.Store({ route: '/systemDoc' });
  @observable searchPage = new SearchPage.Store({ route: '/systemDoc/history' });
  @observable addStore = new AddStore({parent: this, path: '/systemDoc/add',saveBackFunc: this.addBack});
  
  constructor(params) {
    if (params) {
      const {systemDocStore} = params;
      this.systemDocStore = systemDocStore;
    }
  }

  @action
  // 列表模式的读取第一页的文档列表
  getFirstPage = (params) => {
    this.docPageNum = 0;
    this.docList = [];
    this.searchKey = params ? params : {};
    this.getNextPage(params).then(()=>{
      if(this.searchKey.bySearch === 1){
        this.getHistory();
      }
    });
    this.docListViewPageStore.updateDataSource();
  }
  //下拉刷新
  refreshLoading = (params) =>{
    if(this.docPageNum <= 1){
      this.docListViewPageStore.setState({refreshLoading:true});
    }
    this.getFirstPage(params);
  }
  // 列表模式的读取下一页的文档列表
  getNextPage = (_params) => {
    this.docPageNum++;
    let params = {..._params,pageNum: this.docPageNum, pageSize: 20,...this.searchKey};
    return this.getDocList(params)
  }
  
  /**
   * 读取文档列表
   * @param params
   * @returns {PromiseLike<T> | Promise<T> | *}
   */
  getDocList = (params) =>{
    params = {...params,scope:this.systemDocStore.scope};
    let promise = null;
    this.docListViewPageStore.setState({loading: true});
    if(this.systemDocStore.tabKey === 'all'){
      promise = API.getAllDocList(params).then((result) => {
        this.docList = [...this.docList, ...result.docs];
        return result;
      });
    }else if(this.systemDocStore.tabKey === 'my'){
      promise = API.getMyDocList(params).then((result) => {
        this.docList = [...this.docList, ...result.docs];
        return result;
      });
    }else if(this.systemDocStore.tabKey === 'collect'){
      promise = API.getCollectDocList(params).then((result) => {
        this.docList = [...this.docList, ...result.docs];
        return result;
      });
    }
    return promise.then((result) => {
      this.docListViewPageStore.setState({
        loading: false,
        data: toJS(this.docList),
        isTable: false,
        hasMore: result.docs.length > 0,
      });
      this.docListViewPageStore.updateDataSource();
    });
  }
 //将已经阅读过的设置已阅，取消阅读标识
  setDocIsNew = (docid) =>{
    this.docListViewPageStore.data.map((val)=>{
      if(val.docid===docid){
        val.isnew = '0';
      }
      return val;
    })
    this.docListViewPageStore.updateDataSource();
  }

  //获取搜索历史
  getHistory = () => {
      const params = {type : this.searchType};
      API.getHistoryList(params).then((result) => {
          this.historyList = [];
          result.historyData.map((data,i)=>{
            return this.historyList.push({
              key : i,
              name : data,
            });
          });
          this.searchPage.list.setState({data : this.historyList}); 
      }); 
  }

  //初始化搜索历史
  setSearchHistory = () => {
    const template = {
      default: [
        {
          key: 'col1',
          configs: [
            {
              key: 'col1_row1',
              configs: [
                { key: 'name' },
              ],
            },
          ],
        },
      ],
    };
    this.searchPage.setState({ template });
    this.getHistory();
  }

  //搜索
  doSearchBySubject = (docsubject) => {
    this.getFirstPage({docsubject,bySearch : docsubject ? 1 : 0});
    this.systemDocStore.searchAdvancedBasicStore.form.updateField("docsubject",docsubject)
  }

  //清空搜索历史
  clearSearchHistory = () => {
    const params = {type : this.searchType};
    API.clearSearchHistory(params).then((result) => {
        this.historyList = [];
        this.searchPage.list.setState({data : this.historyList});
    });
  }
  
  addBack = ({id, history}) => {
    this.getFirstPage();
    history.go(-1);
  }
}